import Vue from 'vue'
import VueRouter from 'vue-router'
import layout from '@/views/layout'
import collect from '@/views/collect'
import detail from '@/views/detail'
import like from '@/views/like'
import user from '@/views/user'
import login from '@/views/login'
import register from '@/views/register'
import article from '@/views/article'
import { getToken } from '@/utils/storage'
import { Toast } from 'vant'
Vue.use(VueRouter)

const routes = [
  { path: '', redirect: '/article' },
  {
    name: 'layout',
    path: '/',
    component: layout,
    children: [
      { name: 'collect', path: 'collect', component: collect },
      { name: 'like', path: 'like', component: like },
      { name: 'article', path: 'article', component: article },
      { name: 'user', path: 'user', component: user }
    ]
  },
  { name: 'detail', path: '/detail/:id', component: detail },
  { name: 'login', path: '/login', component: login },
  { name: 'register', path: '/register', component: register }
]
const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  const whiteList = ['/login', '/register']
  if (whiteList.includes(to.path)) {
    next()
  } else {
    if (getToken()) {
      next()
    } else {
      Toast('请去登录')
      next('/login')
    }
  }
})
export default router
